package cn.schoolwow.quickdao.module.initial.flow.table;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.UpgradeRequest;
import cn.schoolwow.quickdao.module.operation.flow.upgrade.DatabaseUpgradeFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.Map;
import java.util.function.Consumer;

public class AfterAutomaticCreateFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        if(quickDAOConfig.upgradeOption.afterAutomaticCreate.isEmpty()){
            flowContext.remark("跳过在自动建表语句之后执行语句");
            return;
        }
        for(Map.Entry<String, Consumer<DAO>> entry:quickDAOConfig.upgradeOption.afterAutomaticCreate.entrySet()){
            UpgradeRequest upgradeRequest = new UpgradeRequest();
            upgradeRequest.comment = entry.getKey();
            upgradeRequest.upgradeConsumer = entry.getValue();

            flowContext.startFlow(new DatabaseUpgradeFlow())
                    .putInstanceData(upgradeRequest)
                    .execute();
        }
    }

    @Override
    public String name() {
        return "自动建表语句执行之前";
    }
}
